import { ref, onMounted } from 'vue'
import { getAllEventHttp, putEventsStatusHttp } from '../../request/eventHttp'

export const useEventStore = () => {
  const isAddList = ref(true)
  const isAddEvent = ref(false)
  const tableShow = ref(true)
  const tableData = ref([])
  const currentPage = ref(1)
  const pageSize = ref(5)
  const total = ref()
  const searchEvent = ref('')

  const fetchEvenetData = async () => {
    const res = await getAllEventHttp(currentPage.value, pageSize.value, '', '')
    if (res.data.code === 0) {
      tableData.value = res.data.data
      total.value = res.data.total
    }
  }

  // 分页器size
  const handleSizeChange = (val) => {
    pageSize.value = val
    fetchEvenetData()
  }

  // 分页器page
  const handleCurrentChange = (val) => {
    currentPage.value = val
    fetchEvenetData()
  }

  // 修改审核状态
  const handlePass = (row) => {
    putEventsStatusHttp(row.id, 2)
  }
  const handleFail = (row) => {
    putEventsStatusHttp(row.id, 1)
    fetchEvenetData()
  }

  // 搜索事件
  const handleSearch = async (searchEvent) => {
    if (searchEvent) {
      const res = await getAllEventHttp(currentPage.value, pageSize.value, searchEvent, '')
      if (res.data.code === 0) {
        tableData.value = []
        tableData.value = res.data.data
        total.value = res.data.total
      } else {
        ElMessage.error('搜索的用户不存在')
      }
    } else {
      ElMessage.error('输入内容不能为空')
    }
  }

  // 加载事件数据
  onMounted(() => {
    fetchEvenetData()
  })

  return {
    tableShow,
    isAddList,
    isAddEvent,
    tableData,
    currentPage,
    pageSize,
    total,
    searchEvent,
    fetchEvenetData,
    handleSizeChange,
    handleCurrentChange,
    handlePass,
    handleFail,
    handleSearch
  }
}
